package com.utils
{
	public class RegExpUtil
	{
		private static const PREFIX_TAG:String = '{!';
		private static const SUFFIX_TAG:String = '}';
		private static const REPLACE_STR:String = 'RegExpUtil';
		private static var _replacePairs:Array = ['<br />','<br/>','<br>','\n'];
		private static var _replaceStr:String;
		private static var _resultStr:String; 
		
		public function RegExpUtil()
		{
		}
		
		/**
		 * 字符换行
		 * @param source
		 * @param replacePairs //需要替换元素的数组
		 * @param replaceStr //用来替换的字符
		 * @return 数组
		 */		
		public static function stringWrap(source:*,replacePairs:Array = null,replaceStr:String=''):Array{
			if(replaceStr == '') replaceStr = PREFIX_TAG + REPLACE_STR + SUFFIX_TAG;
			_replaceStr = replaceStr;
			if(replacePairs) _replacePairs = replacePairs;
			_resultStr = '';
			
			if(source is String){
				_resultStr = replace(source,_replacePairs);
			}else if(source is Array){
				for (var i:int = 0; i < source.length; i++) 
				{
					_resultStr += replace(source[i],_replacePairs) + _replaceStr;
				}
			}
			return _resultStr.split(_replaceStr);
		}
		
		/**
		 * 字符替换
		 * @param source
		 * @param replacePairs 数组或者Object
		 * @return 
		 */		
		public static function replace(source:String,replacePairs:*):String{
			if(replacePairs is Array){
				for (var i:int = 0; i < replacePairs.length; i++) 
				{
					source = source.replace(new RegExp(replacePairs[i],'g'),_replaceStr);
				}
			}else if(replacePairs is Object){
				for(var key:String in replacePairs){
					source = source.replace(new RegExp(PREFIX_TAG + key + SUFFIX_TAG,'g'),replacePairs[key]);
				}
			}
			return source;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}